﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic;
using umbraco.cms.presentation.Trees;
using Umb.SimpleWebsite.Shared.DataTypes.Trees.FileSO;

namespace Umb.SimpleWebsite.Shared.SiteEvents
{
    public class BackOfficeApplicationBase : ApplicationBase
    {
        public BackOfficeApplicationBase()
        {
            Document.Newing += new EventHandler<DocumentNewingEventArgs>(Document_Newing);
            Document.AfterNew += new EventHandler<NewEventArgs>(Document_AfterNew);
            Document.BeforeDelete += new Document.DeleteEventHandler(Document_BeforeDelete);
            Document.BeforeSave += new Document.SaveEventHandler(Document_BeforeSave);
            Document.BeforeMoveToTrash += new Document.MoveToTrashEventHandler(Document_BeforeMoveToTrash);
            RegisterTrees();
        }

        public static void RegisterTrees()
        {

            TreeDefinition tree=TreeDefinitionCollection.Instance.FindTree("fsobjects");
            if (tree == null)//reregister
            {
                Type ttype=typeof(FSOTree);
                string aname=ttype.Assembly.GetName().Name;
                string tname=ttype.FullName.Substring(aname.Length+1);
                byte sortOrder = 0;
                ApplicationTree atree = new ApplicationTree(true, true,sortOrder, "fsobjects", "fsobjects", "File System Objects", "folder.png", "foldero.png", aname, tname, "");
                 
                Application app = new Application("fsobjects", "fsobjects", "fsobjects.png");
                TreeDefinition treeDefinitionItem = new TreeDefinition(typeof(FSOTree), atree, app); 
                TreeDefinitionCollection.Instance.Add(treeDefinitionItem);
                
                
                tree = TreeDefinitionCollection.Instance.FindTree("fsobjects");
                if (tree == null) throw new NullReferenceException("Tree should be registered and initialized![fsobjects]");
            }
        }

        void Document_Newing(object sender, DocumentNewingEventArgs e)
        {
            
        }

        

        void Document_AfterNew(object sender, NewEventArgs e)
        {            
        }
        void Document_BeforeMoveToTrash(Document sender, MoveToTrashEventArgs e)
        {
            
        }

        void Document_BeforeSave(Document sender, SaveEventArgs e)
        {
            
        }

        void Document_BeforeDelete(Document sender, DeleteEventArgs e)
        {
            
        }
    }
}
